package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import common.atguigu.ssyx.common.utils.MD5;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @作者:胡树苑
 * @时间:2025/9/11 08:35
 */
@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/acl/user")
public class AdminController {
    @Autowired
    private AdminService adminService;
    @Autowired
    private RoleService roleService;
    //为用户分配角色
//    url: `${api_name}/doAssign`,
//    method: 'post',
//    params: {
//        adminId,
//                roleId

    @ApiOperation("为用户分配角色")
    @PostMapping("/doAssign")
    public Result doAssign(@RequestParam("adminId") Long adminId,
                           @RequestParam("roleId") Long[] roleIds) {
        roleService.saveAdminRole(adminId,roleIds);
        return Result.ok(null);
    }




    //获取所有角色,和根据用户id查询用户分配角色列表
//    url: `${api_name}/toAssign/${adminId}`,
//    method: 'get'
    @ApiOperation("获取用户角色")
    @GetMapping("/toAssign/{adminId}")
    public Result toAssign(@PathVariable("adminId") Long adminId) {
        //返回map集合包含两个角色:所有角色和为用户分配的角色列表
       Map<String,Object> map = roleService.getRoleByAdminId(adminId);
       return Result.ok(map);
    }

    //1.用户列表
    @ApiOperation("用户列表(带搜索)")
    @GetMapping("/{page}/{limit}")
    public Result getPageList(@PathVariable("page") Long page,
                              @PathVariable("limit") Long limit,
                              AdminQueryVo adminQueryVo) {
        //创建分页对象，传递当前页和每页记录数
        //current:当前页
        //limit:每页显示记录数
        Page<Admin> pageparam = new Page<>(page, limit);
        //调用service方法实现条件分页查询，返回分页对象
        IPage<Admin> pageModel  = adminService.selectAdminPage(pageparam,adminQueryVo);
        return Result.ok(pageModel);
    }
    //2.id查询用户
    @ApiOperation("id查询用户")
    @GetMapping("/get/{id}")
    public Result<Admin> getById(@PathVariable Long id) {
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }
    //3.添加用户
    @ApiOperation("添加用户")
    @PostMapping("/save")
    public Result save(@RequestBody Admin admin) {
        //获取输入的密码
        String password = admin.getPassword();
        //对输入的密码进行md5操作
        String passwordMD5 = MD5.encrypt(password);
        //设置到admin对象里面
        admin.setPassword(passwordMD5);
        //调用方法添加
        boolean is_success = adminService.save(admin);
        if (is_success) {
            return  Result.ok(null);
        }else{
            return Result.fail(null);
        }
    }
    //4.修改用户
    @ApiOperation("修改用户")
    @PutMapping("/update")
    public Result update(@RequestBody Admin admin) {
//        修改不能改密码
//        //获取输入的密码
//        String password = admin.getPassword();
//        //对输入的密码进行md5操作
//        String passwordMD5 = MD5.encrypt(password);
//        //设置到admin对象里面
//        admin.setPassword(passwordMD5);
        boolean is_success = adminService.updateById(admin);
        if (is_success) {
            return  Result.ok(null);
        }else {
            return Result.fail(null);
        }
    }
    //5.根据id删除用户
    @ApiOperation("根据id删除用户")
    @DeleteMapping("/remove/{id}")
    public Result remove(@PathVariable Long id) {
        boolean is_success = adminService.removeById(id);
        if (is_success) {
            return  Result.ok(null);
        }else{
            return Result.fail(null);
        }
    }
    //6.批量删除
    @ApiOperation("批量删除")
    @DeleteMapping("/batchRemove")
    public Result batchRemove(@RequestBody List<Long> ids) {
        boolean is_success = adminService.removeByIds(ids);
        if (is_success) {
            return  Result.ok(null);
        }else{
            return Result.fail(null);
        }
    }
}
